//
//  WBStatusViewModel.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/30.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import Foundation

/*
 打印需要的调试模型信息
 遵守 协议
 
 返回 计算型属性
 
 
优化的目的
 尽量少的计算，提前计算好
 控制不设圆角，渲染属性 都要注意
 不要动态创建控件，提前创建好，需要时显示，隐藏
 cell内的层次，越少越好
 要测量，不要猜测
 */

//单条“视图 模型”
class WBStatusViewModel :CustomStringConvertible {
    
    var status: WBStatus
    
    var membericon: UIImage?
    
    var vipimg: UIImage?
    //转发
    var retweeteStr: String?
    //评论
    var commentStr: String?
    //点赞
    var likeStr: String?
    
    //配图大小
    var pictureViewSize = CGSize()
    
    //转发的一定没有图
    var picURLS:[WBStatusPicture]?{
        /*
         有被转发微博，返回被转发的微博配图
         没有转发微博，返回原创微博配图
         都没有返回nil
         */
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    //被转发微博文本
    var retweetedText:String?
    
    //行高
    var rowHeight: CGFloat = 0.0
    
    
    //传入数字，显示描述文本
    fileprivate func countstring(count:Int,defaultString:String) ->String {
        if count == 0 {
            return defaultString
        }
        if count < 10000 {
            return count.description
        }
        return String(format: "%0.2f 万",count/10000)
        
    }
    
    /// 构造函数
    ///
    /// - Parameter model: 单条微博“数据模型”
    init(model:WBStatus) {
        self.status = model
        
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            
            let imagname = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            membericon = UIImage(named: imagname)
        }
        
        switch model.user?.verified_type ?? -1 {//没认证
        case 0://认证
            vipimg = UIImage(named: "common_icon_membership_level4")
        case 2,3,5://企业认证
            vipimg = UIImage(named: "common_icon_membership_level2")
        case 220://达人
            vipimg = UIImage(named: "common_icon_membership_level7")
        default:
            break
        }
        
        retweeteStr = countstring(count: model.reposts_count, defaultString: "转发")
        commentStr = countstring(count: model.comments_count, defaultString: "评论")
        likeStr = countstring(count: model.attitudes_count, defaultString: "赞")
    
//        计算视图大小:有转发微博计算转发微博，没有计算原创微博
        pictureViewSize = calePictureViewSize(count: picURLS?.count)
    
        //设置被转发微博的文本
        retweetedText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        
        //计算行高
        updateRowHeight()
    }
    
    
    fileprivate func calePictureViewSize(count:Int?)->CGSize {
        
        if count == 0 || count == nil{
            return CGSize()
        }
        
    //1.计算宽度：常数，
        //计算高度，count 计算行数
        let row = (count! - 1) / 3 + 1
        
        let height = CGFloat(WBStatusPictureViewOutMargin) + CGFloat(row) * WBStatusPictureViewItemWidth + CGFloat(row - 1) * CGFloat(WBStatusPictureViewInnerMargin)

        return CGSize(width: WBStatusPictureViewWidth, height: height)
    }
    
    var description: String{
        return status.description
    }
    
    func updateSingleImageSize(image:UIImage) {
        
        var size = image.size
        
        //过宽处理
        let maxWidth:CGFloat = 300
        if size.width > maxWidth {
            size.width = maxWidth
            //特殊处理高度，否则高度过高，影响用户体验＝／4
            size.height = size.width * image.size.height / image.size.width/4
        }
        //过窄的处理
        let minWidth:CGFloat = 40
        if size.width < minWidth {
            size.width = minWidth
            size.height = size.width * image.size.height / image.size.width
        }
        
        //特例本身过窄，很长－定义一个ninheight，思路同上
        
        
        
        
        
        size.height += CGFloat(WBStatusPictureViewOutMargin)
        
        pictureViewSize = size
        
        //计算行高
        updateRowHeight()
    }
    
    //内容计算行高
    func updateRowHeight() {
        //原创
        //转发微博
        let margin:CGFloat = 12
        let iconHeight: CGFloat = 34
        let toobarHeight:CGFloat = 35
        
        var height:CGFloat = 0
        
        //文本宽度，计算size
        let viewHeight = CGSize(width: UIScreen.main.bounds.size.width - 2 * margin, height: CGFloat(MAXFLOAT))
        let originFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        
        //1.顶部位置
        height = 2 * margin + iconHeight + margin
        
        //2.正文高度
        if let text = status.text {
            
            height += (text as NSString).boundingRect(with: viewHeight, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName:originFont], context: nil).height
        }
        
        //是否是转发微博
        if status.retweeted_status != nil {
            height += 2 * margin
            
            if let text = retweetedText {
                
                height += (text as NSString).boundingRect(with: viewHeight, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName:retweetedFont], context: nil).height
            }
            
        }
        
        //配图视图高度
        height += pictureViewSize.height
        
        height += margin
        
        //底部工具栏
        height += toobarHeight
        
        rowHeight = height
    }
}
